package com.schoolET.service.wechat.wechatImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.schoolET.entity.Wechat;
import com.schoolET.entity.WechatAndRegion;
import com.schoolET.entity.dto.WechatInfoDTO;
import com.schoolET.entity.vo.EditWechatInfoVO;
import com.schoolET.error.costom.NoWechatException;
import com.schoolET.mapper.WechatAndRegionMapper;
import com.schoolET.mapper.WechatMapper;
import com.schoolET.service.wechat.WechatClientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: lin
 * @className: UserLoginServiceImpl
 * @createTime: 2024-06-05  19:17
 * @description: 用户服务
 */
@Service
public class WechatClientServiceImpl implements WechatClientService {

    @Resource
    private WechatMapper wechatMapper;

    @Resource
    private WechatAndRegionMapper wechatAndRegionMapper;

    @Override
    public boolean getOpenId(String openId) {
        QueryWrapper<Wechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId);
        Wechat wechat = wechatMapper.selectOne(queryWrapper);
        if (wechat == null) {
            Wechat wechat1 = new Wechat();
            wechat1.setOpenId(openId);
            wechatMapper.insert(wechat1);
            return false;
        } else {
            WechatAndRegion wechatAndRegion = wechatAndRegionMapper.selectByWechatId(wechat.getId());
            return wechatAndRegion != null;
        }
    }

    @Override
    public WechatInfoDTO getWechatInfo(String openId) {
        return wechatMapper.selectByOpenId(openId);
    }

    @Override
    public boolean editWechatInfo(String openId, EditWechatInfoVO editWechatInfoVO) {
        if (wechatMapper.selectByOpenId(openId) == null) {
            throw new NoWechatException();
        }
        wechatMapper.updateWechatInfo(openId, editWechatInfoVO.getNickName(), editWechatInfoVO.getPhone());
        return true;
    }
}
